package com.ruoyi.web.controller.example;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.BizException;
import com.ruoyi.NewProduct.domain.NewProduct;
import com.ruoyi.NewProduct.service.NewProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Anonymous
@Api(tags = "店铺管理")
@RestController
public class NewResShopController {

    @Autowired
    NewProductService service;

    @Autowired
    RedisCache redis;
    //三级第一个接口
    @GetMapping ("/GetShopList")
    @ApiOperation(value="查询所有店铺的接口")
    public AjaxResult GetShopList() {
        return AjaxResult.success(service.getShopList());
    }

    //三级第二个
    @ApiOperation(value = "查询某个店铺详情")
    @GetMapping("/GetShopDetailByIdName")
    public AjaxResult GetShopDetailByIdName(String name){
        return AjaxResult.success(service.GetShopDetailByIdName(name));

    }
    //三级删除

    //if
    @PostMapping("/getShopByIf")
    @ApiOperation(value = "/获取店铺信息-if")
    public AjaxResult getShopByIf(@RequestBody NewProduct re) {


        if(re.getId() <=0){
            throw new BizException(500,"id不能为空");
        }

        NewProduct result =service.getShopByid(re.getId());
        if(null ==result){
            throw new BizException(500,"该店铺id不存在");
        }
        return AjaxResult.success(service.selectShopByIf(re));
    }

    //when
    @PostMapping("/getShopByWhen")
    @ApiOperation(value = "/获取店铺信息-when")
    public AjaxResult getShopByWhen(@RequestBody NewProduct re) {
        return AjaxResult.success(service.selectShopByWhen(re));
    }

    @PostMapping("/InsertresShop")
    @ApiOperation(value = "添加店铺的接口")
    public AjaxResult InsertresShop(@RequestBody NewProduct resShop) {
        return AjaxResult.success(service.insertNewShop(resShop));
    }
    @PutMapping ("/updateresShop")
    @ApiOperation(value = "修改店铺的接口")
    public AjaxResult updateresShop(@RequestBody NewProduct resShop){
        return  AjaxResult.success(service.updateNewshop(resShop));
    }
    @DeleteMapping("/DeteleresShop")
    @ApiOperation(value = "删除店铺的接口")
    public AjaxResult DeleteresShop(int id){
        return  AjaxResult.success(service.deleteNewShop(id));
    }

    //redis
    @GetMapping("/testRedis")
    @ApiOperation(value = "测试redis")
    public AjaxResult testRedis (String name){
        //先获取对象
        //针对redis key设置过期时间
        redis.setCacheObject("myName",name,20, TimeUnit.SECONDS);
        return AjaxResult.success("姓名存入redis成功");
    }


    //体会redis缓存作用
    //redis在实际的开发中，通常以缓存的形式存在
    //缓存的作用，就是为了让后续的查询，能够快速查到结果
    //保护数据库
    @GetMapping("/GetShopByName")
    @ApiOperation(value = "redis作为缓存")
    public AjaxResult GetshopByName(String name){
        String result = redis.getCacheObject(name);
        if (result != ""&& result != null){

            return AjaxResult.success("我是直接从redis获取的"+result);
        }

        NewProduct re = new NewProduct();
        re.setName(name);
        List<NewProduct> list = service.selectShopByIf(re);
        String json = JSON.toJSONString(list);
        redis.setCacheObject(name,json);
        return  AjaxResult.success(service.selectShopByIf(re));
    }

}
